package com.exartisan.molisasurvey;

import java.io.IOException;

import com.exartisan.datamanager.SurveyManager;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.widget.Toast;

public class SplashActivity extends Activity {

	 protected boolean _active = true;
	    protected int _splashTime = 3000;
	    SurveyManager surveyManager = SurveyManager.getInstance();
	    Handler handler = new Handler(){
	        @Override
	         public void handleMessage(Message msg) {
	             // TODO Auto-generated method stub
	             super.handleMessage(msg);
	             Resources res = getResources(); //if you are in an activity
	             AssetManager am = res.getAssets();
	             String fileList[];
				try {
					fileList = am.list("data");
					 if (fileList != null)
	                 {   
	                     for ( int i = 0;i<fileList.length;i++)
	                     {
//	                    	 Toast.makeText(getApplicationContext(), fileList[i], Toast.LENGTH_LONG).show();
//	                    	 surveyManager.addSurveyFromAssets(getApplicationContext(), fileList[i]);
	                     }
	                 }
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	    
//				surveyManager.addSurveyFromAssets(getApplicationContext(), "phan1.xml");
//				 surveyManager.addSurveyFromAssets(getApplicationContext(), "phan2.xml");
	             Intent intent = new Intent(SplashActivity.this, LoginActivity.class);                    
	             startActivity(intent);             
	         } 
	     } ;
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_splash);        
	        Thread splashTread = new Thread() {
	            @Override
	            public void run() {        	
	                try {                	                	
	                    int waited = 0;
	                    while(_active && (waited < _splashTime)) {
	                        sleep(100);
	                        if(_active) {
	                            waited += 100;
	                        }
	                    }
	                } catch(InterruptedException e) {
	                    // do nothing
	                } catch (Exception e) {
					
			} finally {
	                    finish();                   
	                }			               		
	                handler.sendEmptyMessage(1);				
	            }
	        };
	        splashTread.start();
	    }
	    @Override
	    public boolean onTouchEvent(MotionEvent event) {
	        if (event.getAction() == MotionEvent.ACTION_DOWN) {
	            _active = false;
	        }
	        return true;
	    }

}
